package com.software.lease.web.admin.custom.schedule;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.software.lease.model.entity.LeaseAgreement;
import com.software.lease.model.enums.LeaseStatus;
import com.software.lease.web.admin.service.LeaseAgreementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author：wzy
 * @Date: 2024/10/10 9:11
 */
@Component
@Slf4j
public class LeaseSchedule {

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    /**
     * 每分钟执行一次
     */
    @Scheduled(cron = "0 * * * * *")
    public void testSchedule(){
        log.info("定时任务执行: {}",new SimpleDateFormat("yyyy-MM-hh").format(new Date()));

    }

    @Scheduled(cron ="0 0 1 * * *")// 每天0点0时0分执行
    public void agreementSchedule(){
        LambdaUpdateWrapper<LeaseAgreement> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(LeaseAgreement::getStatus, LeaseStatus.SIGNED);
        wrapper.le(LeaseAgreement::getLeaseEndDate,new Date());
        wrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        leaseAgreementService.update(wrapper);

    }
}
